#ifndef WEBRTC_TEST_HPP
#define WEBRTC_TEST_HPP

#include "rtc/rtc.hpp"
#include <nlohmann/json.hpp>
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <thread>
#include <chrono>
#include <atomic>
#include <mutex>
#include <random>
#include <iomanip>
#include <sstream>
#include <memory>
#include <future>
#include <vector>
#include <fstream>
#include <signal.h>
#include <cstdlib>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>


using std::shared_ptr;
using nlohmann::json;

typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;

class WebRTC_offer {
public:
    WebRTC_offer();
    ~WebRTC_offer();

    void create_offer();
    void set_local_description(const std::string& sdp);
    void on_ice_candidate(const std::string& candidate);
    
    // 获取房间ID的函数
    std::string get_room_id();
    
    // WebSocket心跳相关函数
    void start_heartbeat(const std::string& room_id, const std::string& websocket_url = "wss://information-channel.cn/back-end/rt-signaling/signaling?token=valid_token&clientId=robot&clientType=HARDWARE_DEVICE");
    void stop_heartbeat();
    
    // WebRTC相关函数
    void init_webrtc();
    void connect_signaling_server(const std::string& room_id);
    void create_webrtc_offer();
    void handle_answer(const json& answer_data);
    void handle_ice_candidate(const json& candidate_data);

        // 发送房间相关动作消息（如加入房间、离开房间）
    void send_room_action(const std::string& action_type);
    
    // PCD文件处理功能
    bool load_pcd_file(const std::string& file_path);
    std::vector<uint8_t> pcd_to_binary();
    std::vector<std::vector<uint8_t>> slice_binary_data(const std::vector<uint8_t>& data, size_t max_chunk_size = 64 * 1024);
    
    // 数据传输功能
    void send_binary_data_via_rtc(const std::vector<uint8_t>& data);
    std::vector<uint8_t> create_data_packet(uint32_t message_id, uint32_t index, uint32_t total, uint32_t data_type, const std::vector<uint8_t>& data);
    void print_packet_header(const std::vector<uint8_t>& packet);
    void wait_for_buffer_available(); // 等待RTC缓存区可用
    
    // WebSocket消息处理

private:
    shared_ptr<rtc::PeerConnection> peer_connection_;
    shared_ptr<rtc::WebSocket> websocket_;
    shared_ptr<rtc::DataChannel> data_channel_;
    json offer_;
    
    // HTTP回调函数，用于接收服务器响应
    static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp);
    
    // 心跳相关成员变量
    std::atomic<bool> heartbeat_running_;
    std::thread heartbeat_thread_;
    std::string robot_id_;
    std::string current_room_id_;
    std::mutex heartbeat_mutex_;
    
    // ICE candidates 收集
    std::vector<std::string> local_ice_candidates_;
    std::mutex ice_candidates_mutex_;
    std::atomic<bool> ice_gathering_complete_;
    
    // PCD点云数据
    pcl::PointCloud<pcl::PointXYZ>::Ptr point_cloud_;
    std::vector<uint8_t> binary_data_;
    static const size_t MAX_DATA_CHANNEL_SIZE = 64 * 1024;  // 64KB
    static const size_t MAX_BUFFER_SIZE = 64 * 1024 * 1024; // 64MB
    static const size_t RTC_BUFFER_THRESHOLD = 10 * 1024 * 1024; // 10MB RTC缓存区阈值
    
    // WebSocket相关函数
    void heartbeat_loop(const std::string& room_id, const std::string& websocket_url);
    std::string generate_message_id();
    std::string get_iso8601_timestamp();
    json create_heartbeat_message(const std::string& room_id);
    
    // WebRTC信令消息创建函数
    json create_send_info_message(const std::string& room_id, const json& data);
    json create_offer_data(const std::string& offer_sdp);
    json create_ice_candidate_batch_data(const std::vector<std::string>& ice_candidates);
    
    // ICE candidate 处理
    void send_ice_candidate_batch();
    
    // WebSocket消息处理
    void handle_websocket_message(const std::string& message);
    
};


#endif // WEBRTC_TEST_HPP